<?php

namespace yiike\credit\models;

use Yii;
use \yiike\credit\models\base\Credit as BaseCredit;

/**
 * This is the model class for table "credit".
 */
class Credit extends BaseCredit {
    
    public $secretKey = 'asdfanev';

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'title'    => '标题',
            'value1'   => '积分',
            'value2'   => '金币',
            'value3'   => '钻石',
            'user_id'  => '用户',
            'password' => '支付密码',
            'memo'     => '备注',
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeHints()
    {
        return
            [
            'title'    => '标题',
            'value1'   => '积分',
            'value2'   => '金币',
            'value3'   => '钻石',
            'user_id'  => '用户',
            'password' => '支付密码',
            'memo'     => '备注',
            ];
    }
    
    public function rules() {
        $rules = parent::rules();
        return $rules + [
            [['user_id'], 'required', 'on' => 'create'],
            [['value1'], 'required', 'on' => 'deposit'],
            [['user_id'], 'required', 'on' => 'transfer'],
        ];
    }

    public function beforeSave($insert) {
        $this->value1_encrypt = $this->hashValue($this->value1);
        $this->value2_encrypt = $this->hashValue($this->value2);
        $this->value3_encrypt = $this->hashValue($this->value3);
        return parent::beforeSave($insert);
    }

    public static function findOne($condition) {
        if (! $condition) return NULL;
        
        $find = parent::findOne($condition);
        
        if ($find) {
            return $find;
        }
        else {
            $model = new Credit();
            if (is_array($condition)) {
                $model->attributes = $condition;
            }
            else {
                $model->user_id = $condition;
            }
            $model->value1 = 0;
            $model->value2 = 0;
            $model->value3 = 0;
            $model->save();
            return $model;
        }
    }
    
    function hashValue($value) {
        return hash_hmac('ripemd160', $value + 0, $this->secretKey . $this->user_id);
    }

    function verifyEncrypt() {

        if ($this->value1 > 0 && $this->hashValue($this->value1) != $this->value1_encrypt) {
            $label = $this->attributeLabels()['value1'];
            $this->addError($this->value1, "{$label}余额检验失败");
            return false;
        }

        if ($this->value2 > 0 && $this->hashValue($this->value2) != $this->value2_encrypt) {
            $label = $this->attributeLabels()['value1'];
            $this->addError($this->value2, "{$label}余额检验失败");
            return false;
        }

        if ($this->value3 > 0 && $this->hashValue($this->value3) != $this->value3_encrypt) {
            $label = $this->attributeLabels()['value1'];
            $this->addError($this->value3, "{$label}余额检验失败");
            return false;
        }

        return TRUE;
    }
}
